// pages/beauty/index/index.js
const common = require('../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        navType: 0,
        imgSwiper: [],
        doctorList: [],
        beautyList: [],
        exampleList: [],
        nowPage: 0,
        isLoading: false,
        isAll: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getImgSwiper()
        this.getDoctorList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.setData({
            doctorList: [],
            beautyList: [],
            exampleList: [],
            nowPage: 0,
            isLoading: false,
            isAll: false
        })
        common.refresh()
        setTimeout(function () {
            wx.stopPullDownRefresh()
        }, 2000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if (this.data.navType == 0) {
            this.getDoctorList()
        }
        else if (this.data.navType == 1) {
            this.getBeautyList()
        }
        else if (this.data.navType == 2) {
            this.getExampleList()
        }
    },

    getImgSwiper: function () {
        const that = this
        let data = {
            activity: 785,
            status: 2,
            page: 0,
            limit: 99
        }
        common.post('Slideshow/slideshow_list', data).then(function (res) {
            if (res.code == 204) {
                return
            }
            that.setData({
                imgSwiper: res
            })
        }).catch(function (e) { })
    },

    getDoctorList: function () {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中',
        })
        const that = this
        let doctorList = this.data.doctorList
        let data = {
            activity: 785,
            page: this.data.nowPage,
            limit: 10
        }
        common.post('Doctor/get_doctor', data).then(function (res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    doctorList: doctorList,
                    isAll: true
                })
                return
            }
            doctorList = doctorList.concat(res)
            that.setData({
                doctorList: doctorList,
                nowPage: that.data.nowPage + 1
            })
        }).catch(function (e) { })
    },

    getBeautyList: function () {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中',
        })
        const that = this
        let beautyList = this.data.beautyList
        let data = {
            activity: 785,
            is_sale: '1',
            is_goods: '11',
            page: this.data.nowPage,
            limit: 10
        }
        common.post('Goodslist/goods_list', data).then(function (res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    beautyList: beautyList,
                    isAll: true
                })
                return
            }
            beautyList = beautyList.concat(res)
            that.setData({
                beautyList: beautyList,
                nowPage: that.data.nowPage + 1
            })
        }).catch(function (e) { })
    },

    getExampleList: function () {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中',
        })
        const that = this
        let exampleList = this.data.exampleList
        let data = {
            activity: 785,
            page: this.data.nowPage,
            limit: 10
        }
        common.post('Doctor/get_doctor_case', data).then(function (res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    exampleList: exampleList,
                    isAll: true
                })
                return
            }
            exampleList = exampleList.concat(res)
            that.setData({
                exampleList: exampleList,
                nowPage: that.data.nowPage + 1
            })
        }).catch(function (e) { })
    },

    navClick: function (e) {
        if (this.data.navType == e.currentTarget.dataset.type) {
            return
        }
        this.setData({
            navType: e.currentTarget.dataset.type,
            doctorList: [],
            beautyList: [],
            exampleList: [],
            nowPage: 0,
            isLoading: false,
            isAll: false
        })
        if (e.currentTarget.dataset.type == 0) {
            this.getDoctorList()
        }
        else if (e.currentTarget.dataset.type == 1) {
            this.getBeautyList()
        }
        else if (e.currentTarget.dataset.type == 2) {
            this.getExampleList()
        }
    },

    goGoodsDetail: function (e) {
        wx.navigateTo({
            url: '/pages/goods/goodsDetail/goodsDetail?goodsId=' + e.currentTarget.dataset.id
        })
    },

    goDoctorDetail: function (e) {
        wx.navigateTo({
            url: '/pages/beauty/doctorDetail/doctorDetail?id=' + e.currentTarget.dataset.id
        })
    },

    goExampleDetail: function (e) {
        wx.navigateTo({
            url: '/pages/beauty/exampleDetail/exampleDetail?id=' + e.currentTarget.dataset.id
        })
    }
})